import { ShoppingService } from './shopping';
import { StationService } from './station';
import * as wuzhui from "maishu-wuzhui";
import { createService } from 'components/common/base-component';
import { DataSource } from 'maishu-wuzhui';
import { ActivityService } from './activity';
import { AccountService } from './account';

let w = wuzhui;
let shopping = createService(ShoppingService);
let station = createService(StationService);
let activity = createService(ActivityService);
let account = createService(AccountService);

// station.error.add((sender, err) => app.error.fire(app, err, app.currentPage));

// export let pageData = new wuzhui.DataSource<PageData>({
//     primaryKeys: ['id'],
//     select: (args) => {
//         //===================================================
//         // 服务端过滤有 BUG，这么写无效
//         // Type is null or Type == 'page'
//         args.filter = `(Type is null) or (Type == 'page')`;
//         //===================================================
//         return station.pageList(args);
//     },
//     insert: (item, args: { isSystem }) => {
//         item.type = 'page';
//         return station.savePageData(item);
//     },
//     update: (item) => {
//         return station.savePageData(item)
//     },
//     delete: (item) => station.deletePageData(item.id)
// })

// export let productTemplate = Object.assign(new w.DataSource<PageData>({
//     primaryKeys: ['id'],
//     select: (args) => station.productTemplateList(args),
//     insert: (item, args: { isSystem }) => station.savePageData(item),
//     update: (item) => station.savePageData(item),
//     delete: (item) => station.deletePageData(item.id)
// }), { count: new chitu.ValueStore() })

let productDataSource: wuzhui.DataSource<Product> = new wuzhui.DataSource<Product>({
    primaryKeys: ['Id'],
    select: (args) => shopping.products(args),
    delete: (item) => shopping.deleteProduct(item.Id),
    insert: (item, args) => {
        args = args || {};
        return shopping.saveProduct({
            product: item, parentId: args.parentId, id: args.id
        })
    },
    update: (item, args) => {
        args = args || {};
        return shopping.saveProduct({
            product: item, parentId: args.parentId, id: args.id
        })
    }
})

let categoryDataSource = new DataSource<Category>({
    primaryKeys: ["Id"],
    select: async (args) => {
        let dataItems = await shopping.categories();
        return { dataItems, totalRowCount: dataItems.length };
    },
    insert: async (item) => {
        let r = await shopping.addCategory(item);
        return r;
    },
    delete: async (item) => {
        await shopping.deleteCategory(item.Id);
    }
});

let brandDataSource = new DataSource<Brand>({
    primaryKeys: ["Id"],
    select: async (args) => {
        let r = await shopping.brands(args);
        return r;
    },
    insert: async (item) => {
        let r = await shopping.addBrand(item);
        return r;
    },
    delete: async (item) => {
        await shopping.deleteBrand(item);
    }
})

let couponDataSource = new DataSource<Coupon>({
    primaryKeys: ['Id'],
    select: (args) => shopping.coupons(args),
    delete: (dataItem) => shopping.deleteCoupon(dataItem),
    update: (dataItem) => shopping.saveCoupon(dataItem),
    insert: (dataItem) => shopping.saveCoupon(dataItem),
})

let activityDataSource = new wuzhui.DataSource<PromotionActivity>({
    primaryKeys: ['Id'],
    select: () => activity.activities(),
    insert: (dataItem) => activity.addActivity(dataItem),
    delete: (dataItem) => activity.deleteActivity(dataItem),
    update: (dataItem) => activity.updateActivity(dataItem)
})

let receivementAccount = new w.DataSource<ReceivementAccount>({
    primaryKeys: ['Id'],
    select: async () => {
        let dataItems = await account.receivementAccountList();
        return { dataItems, totalRowCount: dataItems.length };
    },
    insert: (item) => account.saveReceivementAccount(item),
    update: (item) => account.saveReceivementAccount(item)
});


export let dataSources = {
    product: productDataSource,
    category: categoryDataSource,
    brand: brandDataSource,
    activity: activityDataSource,
    coupon: couponDataSource,
    account: receivementAccount,
}



